<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class model_user extends base_model{

	//hook mod_user_start.php
	private $table;
	private $groups = array();
	private $tuser = array();

	function __construct() {
		//hook mod_user_before_parent_init.php
		parent::__construct();
		$this->table = 'user';
		
		//hook mod_user_before_user_init.php
		$this->user_init();
	}

	public function get_user_by_uid($uid, $p = true) {
		$uid = intval($uid);
		$arr = $this->db->get($this->table. '-uid-'. $uid);
		if(!empty($arr) && $p){
			$this->tuser = $arr;
			return $this;
		}else{
			return $arr;
		}
	}
	
	public function get_user_by_name($name, $p = true) {
		$name = str_replace('-','',$name);
		$arr = $this->db->get($this->table. '-name-'. $name);
		if(!empty($arr) && $p){
			$this->tuser = $arr;
			return $this;
		}else{
			return $arr;
		}
	}
	
	public function get_user_by_ssid($ssid) {
		$ssid = str_replace('-','',$ssid);
		$arr = $this->db->get($this->table. '-ssid-'. $ssid);
		if(!empty($arr))$this->tuser = $arr;
		return $this;
	}
	//提供链式操作
	
	public function is_logined() {
		return isset($this->tuser['groupid']) && ($this->tuser['groupid'] > 0 );
	}
	
	public function is_locked() {
		return isset($this->tuser['groupid']) && ($this->tuser['groupid'] == 200 );
	}
	
	public function user_uid() {
		return $this->tuser['uid'];
	}
	
	public function user_groupid() {
		return $this->tuser['groupid'];
	}
	
	public function user_name() {
		return $this->tuser['name'];
	}
	
	public function user_core() {
		$arr = array();
		$arr['uid'] = $this->tuser['uid'];
		$arr['groupid'] = $this->tuser['groupid'];
		$arr['core'] = $this->tuser['core'];
		$arr['visitc'] = $this->tuser['visitc'];
		$arr['name'] = $this->tuser['name'];
		//hook mod_user_user_core_end.php
		return $arr;
	}
	
	public function base_info() {
		$arr = array();
		$arr['phone'] = $this->tuser['phone'];
		$arr['email'] = $this->tuser['email'];
		$arr['lastip'] = $this->tuser['lastip'];
		$arr['laststamp'] = $this->tuser['laststamp'];
		$arr['headphoto'] = $this->tuser['headphoto'];
		//hook mod_user_base_info_end.php
		return $arr;
	}
	
	public function other_info() {
		//hook mod_user_other_info_end.php
		return core::json_decode($this->tuser['infos']);
	}
	
	public function user_core_edit($opt = '+', $num = 1, $uid = 0, $core = 0) {
		$core = $core==0 ? $this->tuser['core']:$core;
		$uid = $uid==0 ? $this->tuser['uid']:$uid ;
		$num = intval($num);
		$core = $opt == '+' ? $core + $num : $core - $num;
		$core = max(0, $core);
		
		//hook mod_user_user_core_edit_end.php
		return $this->db->update($this->table. "-uid-$uid",
						array('core'=>$core) );
	}
	
	//=====================>
	
	public function reg_user($name = '',$phone = '',$email = '',
								$pass = '',$ip = '', $infos = array() )
	{
		//$name = str_replace('-','',$name);
		$ssid = $this->make_ssid($name ,$pass);
		$info = core::json_encode($infos);
		$uid = $this->db->maxid($this->table. '-uid', '+1');
		
		//hook mod_user_reg_user_before.php
		if($this->db->set($this->table. '-uid-'.$uid, 
						array('name'=>$name, 'phone'=>$phone, 'email'=>$email,
								'ssid'=>$ssid,'lastip'=>$ip,'headphoto'=>'',
								'infos'=>$info)
				)){
			$this->db->count($this->table. '-uid', '+1');
			return $ssid;
		}else{
			return false;
		}
	}
	
	public function edit_user($uid,$visitc,$groupid = 1,
						$phone = '',$email = '',$pass = '',$name = '',$ip = '')
	{
		$user_edit = array();
		$uid = intval($uid);
		//$name = str_replace('-','',$name);
		
		if($visitc > 0)		$user_edit['visitc'] = $visitc;
		if($groupid >= 0)	$user_edit['groupid'] = $groupid;
		if(!empty($phone))	$user_edit['phone'] = $phone;
		if(!empty($email))	$user_edit['email'] = $email;
		if(!empty($pass))	$user_edit['ssid'] = $this->make_ssid($name ,$pass);
		if(!empty($ip))		$user_edit['lastip'] = $ip;
		
		//hook mod_user_edit_user_end.php
		return $this->db->update($this->table. '-uid-'.$uid, $user_edit);
	}
	
	public function edit_user_infos($uid,$infos = array() )
	{
		$info = core::json_encode($infos);
		//hook mod_user_edit_user_infos.php
		return $this->db->update($this->table. '-uid-'.$uid, 
							array('infos'=>$info) );
	}
	
	public function is_online($last_timestamp) {
		$last_timestamp = str_replace('<br/>',' ',$last_timestamp);
		//hook mod_user_is_online.php
		return  (time() - strtotime($last_timestamp)) < CONFIG_ONLINE_TIME;
	}
	
	public function make_ssid($name ,$pass) {
		//hook mod_user_make_ssid.php
		return md5(md5($pass.'@'.USER_KEY).$name);
	}
	
	public function edit_user_headphoto($uid,$photo_path = '')
	{
		//hook mod_user_edit_user_headphoto.php
		return $this->db->update($this->table. '-uid-'.$uid, 
							array('headphoto'=>$photo_path) );
	}
	
	public function return_group_name($groupid) {
		return $this->groups["$groupid"];
	}
	
	public function user_format($user_base) {
		
		//hook mod_user_user_format.php
		if($user_base['headphoto'] == '')
			$user_base['headphoto'] = 'jy_src/images/default.jpg';
		
		$user_base['laststamp'] = str_replace(' ','<br/>',$user_base['laststamp']);
		
		return $user_base;
	}
	
	public function user_list($cond, $order, $start, $limit) {
	
		//hook mod_user_user_list.php
		$arr = $this->db->index_fetch($this->table , 'uid'
										, $cond, $order, $start, $limit);
		return $arr;
	}
	
	//====================>private
	private function user_init() {
		$this->tuser['name'] = '游客'.rand(10000,99999);
		$this->tuser['groupid'] = 0;
		$this->tuser['uid'] = 0;
		$this->tuser['core'] = 0;
		$this->tuser['visitc'] = 0;
		$this->tuser['phone'] = '';
		$this->tuser['email'] = '';
		$this->tuser['lastip'] = $_SERVER['ip'];
		$this->tuser['laststamp'] = time();
		$this->tuser['headphoto'] = '';
		
		$this->groups = array('0'=>'游客','1'=>'普通用户','2'=>'图文管理',
						'3'=>'文件管理','4'=>'论坛版主','5'=>'超级管理员',
						'100'=>'创始人','200'=>'封锁用户');
		//hook mod_user_user_init.php
	}

	//hook mod_user_end.php
}
/*{TranslateTagEnd}*/

?>